package com.electric.service;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.electric.model.User;
import com.electric.util.DButil;


/**
 * @author Kevin Zhang
 *
 * 2011-5-10
 */
public class UserService {
	public void add(User u) {
		Connection conn = DButil.createConn();
		String sql = "insert into users(id,username,password,email,create_date,update_date) values (null, ?, ?, ?, CURRENT_TIMESTAMP(),CURRENT_TIMESTAMP())";
		PreparedStatement ps = DButil.prepare(conn, sql);
		try {
			ps.setString(1, u.getUsername());
			ps.setString(2, u.getPassword());
			ps.setString(3, u.getEmail());
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DButil.close(ps);
		DButil.close(conn);
	}
	
	public List<User> list() throws SQLException {
		Connection conn = DButil.createConn();
		String sql = "select * from users";
		PreparedStatement ps = DButil.prepare(conn, sql);
		List<User> users = new ArrayList<User>();
		try {
			ResultSet rs = ps.executeQuery();
			User u = null;
			while(rs.next()) {
				u = new User();
				u.setId(rs.getInt("id"));
				u.setUsername(rs.getString("username"));
				u.setPassword(rs.getString("password"));
				u.setEmail(rs.getString("email"));
				users.add(u);
			}
		} catch (SQLException e) {
			e.printStackTrace();
//			throw(e);
		}
		DButil.close(ps);
		DButil.close(conn);
		return users;
	}
	
	public void delete(User u) {
		deleteById(u.getId());
	}
	
	public void deleteById(int id) {
		Connection conn = DButil.createConn();
		String sql = "delete from users where id = ?";
		PreparedStatement ps = DButil.prepare(conn, sql);
		try {
			ps.setInt(1, id);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DButil.close(ps);
		DButil.close(conn);
	}
	
	public void update(User u) {
		Connection conn = DButil.createConn();
		String sql = "update users set username = ?, password = ? , email = ? ,update_date = CURRENT_TIMESTAMP() where id = ?";
		PreparedStatement ps = DButil.prepare(conn, sql);
		try {
			ps.setString(1, u.getUsername());
			ps.setString(2, u.getPassword());
			ps.setString(3, u.getEmail());
			ps.setInt(4, u.getId());
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DButil.close(ps);
		DButil.close(conn);
	}
	
	public User loadById(int id) {
		Connection conn = DButil.createConn();
		String sql = "select * from users where id = ?";
		PreparedStatement ps = DButil.prepare(conn, sql);
		User u = null;
		try {
			ps.setInt(1, id);
			ResultSet rs = ps.executeQuery();
			
			if(rs.next()) {
				u = new User();
				u.setId(rs.getInt("id"));
				u.setUsername(rs.getString("username"));
				u.setPassword(rs.getString("password"));
				u.setEmail(rs.getString("email"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DButil.close(ps);
		DButil.close(conn);
		return u;
	}
	
	public List<User> verifyEmailAndPass(String email ,String pass) throws SQLException{
		Connection conn = DButil.createConn();
		String sql = "select * from users where email = ? and password = ?";
		PreparedStatement ps = DButil.prepare(conn, sql);
		List<User> users = new ArrayList<User>();
		try {
			ps.setString(1, email);
			ps.setString(2, pass);
			ResultSet rs = ps.executeQuery();
			User u = null;
			while(rs.next()) {
				u = new User();
				u.setId(rs.getInt("id"));
				u.setUsername(rs.getString("username"));
				u.setPassword(rs.getString("password"));
				u.setEmail(rs.getString("email"));
				users.add(u);
			}
		} catch (SQLException e) {
			e.printStackTrace();
//			throw(e);
		}
		DButil.close(ps);
		DButil.close(conn);
		return users;
	}
}
